import requests as rq
from Agent_pond.Agent import AGENT
import numpy as np
from lxml import etree
import threading

def check(ip):
    try:
        r = rq.get('https://baidu.com',proxies={'https': ip},timeout=5)
        print(r.status_code)
        if r.status_code == 200:
            print(f"{ip} 可用")
            iplist_ok.append(ip)
        else:
            print(f"{ip} 不可用")
    except:
        print(f"{ip} 不可用")

iplist_ok = []

iplist = []
for page in range(2):
    r = rq.get(f'http://www.xiladaili.com/https/{page+1}/')
    print(r.status_code)
    # print(r.text)
    iplisti = etree.HTML(r.text).xpath('//tbody/tr/td[1]/text()')
    iplisti = ['https://'+i for i in iplisti]
    iplist += iplisti

print(iplist)

check_thread = []
for ip in iplist:
    t = threading.Thread(target=check,args=(ip,))
    check_thread.append(t)
    t.start()

for t in check_thread:
    t.join()

print(iplist_ok)
print(f'存活率为{round(len(iplist_ok)*100/len(iplist),2)}%')
np.save('IP_pond/IP_https.npy',iplist_ok)